package com.cashmanager.client.view;

import com.cashmanager.client.LiveGrid;
import com.cashmanager.client.RpcServiceManager;
import com.cashmanager.shared.vo.CashierCompany;
import com.cashmanager.shared.vo.URL_PARAM;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.events.RecordClickEvent;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.Layout;

public final class CashierCompanyWindow extends AbstractWindow{
	private final static String COMP_ID = "系统编号";
	private final static String COMP_NAME = "厂家名称";
	private final static String COMP_CONTACT = "厂家联系方式";
	private ListGrid searchGrid;
	private TextItem companyNameTextItem;
	private TextItem companyContactTextItem;
	private IButton addButton;
	private IButton updButton;
	private int tmpId;
	
	private void clearInput(){
		companyNameTextItem.clearValue();
		companyContactTextItem.clearValue();
		tmpId = 0;
	}
	private CashierCompany getInputValue(){
		final String name = companyNameTextItem.getValueAsString();
		final String cont = companyContactTextItem.getValueAsString();
		if (name == null || cont == null){
			return null;
		}
		CashierCompany com = new CashierCompany();
		com.setCompanyName(name);
		com.setCompanyContact(cont);
		return com;
	}
	public CashierCompanyWindow(){
		setTitle("点钞机厂家管理");
		companyNameTextItem = new TextItem(COMP_NAME);
		companyContactTextItem = new TextItem(COMP_CONTACT);
		addButton = new IButton("添加");
		updButton = new IButton("更新");
		updButton.disable();
		addButton.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {				
				CashierCompany com = getInputValue();
				if (com == null){
					SC.say("请输入公司名称和公司联系方式");
					return;
				}				
				RpcServiceManager.getCashierService().addCompany(com, new VoidAsyncCallback());
				
				//TODO revert addbutton
			}
			
		});
		
		
		updButton.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				if (tmpId == 0){
					SC.say("请选中要更新厂家所在的行");
					return;
				}
				CashierCompany com = getInputValue();
				com.setCompanyId(tmpId);
				if (com == null){
					SC.say("请输入公司名称和公司联系方式");
					return;
				}	
				RpcServiceManager.getCashierService().updCompany(com, new VoidAsyncCallback());
				updButton.disable();
				addButton.enable();
			}
			
		});
		addItem(getFormLayout());
		addItem(getListGrid());
	}
	
	class VoidAsyncCallback implements AsyncCallback<Void>{
		@Override
		public void onFailure(Throwable caught) {
			
		}

		@Override
		public void onSuccess(Void result) {
			searchGrid.getDataSource().invalidateCache();
			searchGrid.invalidateCache();
			clearInput();
		}
	}
	private DynamicForm createNewCompanyForm() {		
		DynamicForm newCompanyForm = new DynamicForm();
		newCompanyForm.setWidth(600);
		newCompanyForm.setNumCols(6);
		newCompanyForm.setAlign(Alignment.LEFT);	
		newCompanyForm.setFields(companyNameTextItem, companyContactTextItem);
		return newCompanyForm;
	}
	
	
	static class CashierComDataSource extends DataSource{
		private static CashierComDataSource instance;
		private DataSourceTextField compId = 
				new DataSourceTextField(CashierCompany.Fields.COMPANY_ID,COMP_ID);
		private DataSourceTextField compName =  
				new DataSourceTextField(CashierCompany.Fields.CASHIER_COM_NAME,COMP_NAME);;
		private DataSourceTextField compContact = 
				new DataSourceTextField(CashierCompany.Fields.CASHIER_COM_CONT,COMP_CONTACT) ;
		
		public static CashierComDataSource getInstance() {
			if (instance == null){
				instance = new CashierComDataSource();
			}
			return instance;
		}
		private CashierComDataSource(){
			setClientOnly(true);
		    setID("com");
	        setDataFormat(DSDataFormat.XML);
	        setRecordXPath("/List/Record");
			setFields(compId, compName, compContact);
	        setDataURL(GWT.getModuleBaseURL()+"xmlservlet?param="+URL_PARAM.COMPANY_URL_PARAM);

		}
	}
	
	private Layout getFormLayout() {
		HLayout newComLayout = new HLayout();
		newComLayout.setHeight(30);
		newComLayout.setAlign(Alignment.LEFT);
		newComLayout.addMember(createNewCompanyForm());
		newComLayout.addMember(addButton);
		newComLayout.addMember(updButton);
		newComLayout.setMembersMargin(20);
		return newComLayout;
	}


	private ListGrid getListGrid() {
		searchGrid = new LiveGrid();
		searchGrid.setDataSource(CashierComDataSource.getInstance());
		searchGrid.addRecordClickHandler(new com.smartgwt.client.widgets.grid.events.RecordClickHandler(){


			@Override
			public void onRecordClick(RecordClickEvent event) {
				Record rec = event.getRecord();
				companyNameTextItem.setValue(rec.getAttribute(CashierCompany.Fields.CASHIER_COM_NAME));
				companyContactTextItem.setValue(rec.getAttribute(CashierCompany.Fields.CASHIER_COM_CONT));
				tmpId = Integer.valueOf(rec.getAttribute(CashierCompany.Fields.COMPANY_ID));
				addButton.disable();
				updButton.enable();
			}
			
		});
			
		return searchGrid;
	}
}
